/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.cezary.butler.walili.client.network.INetworkStats;

/**
 *
 * @author czarek
 */
public class TrafficLoggingTask extends TimerTask implements Runnable {

    private static final Logger log = Logger.getLogger(TrafficLoggingTask.class.getName());
    private static final INetworkStats stats = ClientApp.initSpringContext().getBean(INetworkStats.class);
    private final int coveredTime;
    private final double divider;

    protected TrafficLoggingTask(int coveredTime) {
        this.coveredTime = coveredTime;
        this.divider = (double)(coveredTime) / 1000. * 1024.;
    }

    public static Timer scheduleTrafficLogging(long frequency){
        Timer t = new Timer("Network traffic logger");
        t.scheduleAtFixedRate(new TrafficLoggingTask((int)(frequency*1.75)), frequency*2, frequency);
        return t;
    }        
    
    protected double kbytesPerSec=0;
    @Override
    public void run() {
        kbytesPerSec=stats.countStatsFor(coveredTime) / (divider);
        if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, "Current traffic is {0}kbytes/sec", kbytesPerSec);
        }
    }
}